home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 2410 / 2410.xpi / chrome / content / foxmarks-unittest.js < prev    next >
Text File  |  2010-01-28  |  11KB  |  325 lines

  1. /* 
  2.  Copyright 2005-2008 Foxmarks Inc.
  3.  
  4.  foxmarks-unittest.js: Handles all the unit tests
  5.    
  6.  */
  7.  
  8. var gFoxmarksUT = {
  9.     status: function(s){
  10.         if(!s || s.length == 0)
  11.             return;
  12.         var os = Cc["@mozilla.org/observer-service;1"]
  13.             .getService(Ci.nsIObserverService);
  14.         os.notifyObservers(null, "foxmarks-unittest-status",
  15.             s);
  16.         Xmarks.LogWrite(s);
  17.     },
  18.     createTextArray: function(length, charlen,type){
  19.         var a = [];
  20.         var len = length;
  21.  
  22.         var charrange;
  23.         var charoffset;
  24.         switch(type){
  25.             case 'all':
  26.                 charrrange = 917999;
  27.                 charoffset = 1;
  28.                 break;
  29.             case 'ascii':
  30.                 charrange = 126 - 32;
  31.                 charoffset = 32;
  32.                 break;
  33.             case 'alpha':
  34.                 charrange = 122 - 97;
  35.                 charoffset = 97;
  36.                 break;
  37.             case 'numeric':
  38.                 charrange = 57 - 48;
  39.                 charoffset = 48;
  40.                 break;
  41.         }
  42.  
  43.         while(len--){
  44.             var txt = [];
  45.             var ct = charlen;
  46.             while(ct--){
  47.                 var ascii = Math.floor(charrange * Math.random()) + charoffset;
  48.                 txt.push( String.fromCharCode(ascii) );
  49.             }
  50.             a.push(txt.join(""));
  51.         }
  52.         return a;
  53.     },
  54.     testList: [
  55.         // encryption tests
  56.         function(statusFunc, errorFunc, finishFunc){
  57.             var TITLE = "Encryption: ";
  58.             var id = "encrypt";
  59.             var encrypt = CreateAESManager();
  60.  
  61.             statusFunc(TITLE + "Started");
  62.             var ct = Xmarks.gSettings.getUnitTestOption(id);
  63.             if(ct){
  64.                 statusFunc(TITLE + "start (" + ct + ")");
  65.  
  66.                 var list = gFoxmarksUT.createTextArray(ct, 255, "ascii");
  67.                 for(var x = 0; x < ct; x++){
  68.                     if(x % 100 == 0){
  69.                         statusFunc(TITLE + "test #" + x);
  70.                     }
  71.                     var t1 = x == 0 ? list[x]: list[x - 1];
  72.                     var t2 = list[x];
  73.                     var s = encrypt.encrypt(t1, t2);
  74.                     if(t2 != encrypt.decrypt(t1, s)){
  75.                         errorFunc("FAIL: encryptor with password '" + t1 + 
  76.                             "' and string '" + t2 + "'");
  77.                     }
  78.                 }
  79.                 statusFunc(TITLE + "end");
  80.             }
  81.             else
  82.                 statusFunc(TITLE + "skipped");
  83.             statusFunc(TITLE + "Ended");
  84.             finishFunc();
  85.         },
  86.  
  87.         // base64 tests
  88.         function(statusFunc, errorFunc, finishFunc){
  89.             var TITLE = "Base64: ";
  90.             var id = "base64";
  91.  
  92.             statusFunc(TITLE + "Started");
  93.             var ct = Xmarks.gSettings.getUnitTestOption(id);
  94.             if(ct){
  95.                 statusFunc(TITLE + "start (" + ct + ")");
  96.  
  97.                 var list = gFoxmarksUT.createTextArray(ct, 255, "all");
  98.                 for(var x = 0; x < ct; x++){
  99.                     if(x % 100 == 0){
  100.                         statusFunc(TITLE + "test #" + x);
  101.                     }
  102.                     var t = list[x];
  103.                     var s = Base64.encode(t, false);
  104.  
  105.                     if(t != Base64.decode(s)){
  106.                         errorFunc("FAIL: base64 with password '" + t1 + 
  107.                             "' and string '" + t2 + "'");
  108.                     }
  109.                     //Components.utils.forceGC();
  110.                 }
  111.                 statusFunc(TITLE + "end");
  112.             }
  113.             else
  114.                 statusFunc(TITLE + "skipped");
  115.             statusFunc(TITLE + "Ended");
  116.             finishFunc();
  117.         },
  118.  
  119.         // sync-upload-download tests
  120.         function(statusFunc, errorFunc, finishFunc){
  121.             var TITLE = "Sync, Upload, Download: ";
  122.             var id = "sync-upload-download";
  123.  
  124.             statusFunc(TITLE + "Started");
  125.             var ct = Xmarks.gSettings.getUnitTestOption(id);
  126.             if(ct){
  127.                 statusFunc(TITLE + "start (" + ct + ")");
  128.  
  129.                 var server = new SyncServer();
  130.                 server.manual = true;
  131.                 var func = function(status){
  132.                     server.sync("dirty", function(a){
  133.                         if(a){
  134.                             errorFunc(TITLE + "sync returned" + a);
  135.                         }
  136.                         else {
  137.                             server.upload(function(b){
  138.                                 if(b){
  139.                                     errorFunc(TITLE + "upload returned" + b);
  140.                                 } else {
  141.                                     server.download(function(c){
  142.                                         if(c){
  143.                                             errorFunc(TITLE + "download returned" + c);
  144.                                         } else {
  145.                                             ct--;
  146.                                             if(ct){
  147.                                                 func();
  148.                                             } else {
  149.                                                 statusFunc(TITLE + "Ended");
  150.                                                 finishFunc();
  151.                                             }
  152.                                         }
  153.  
  154.                                     });
  155.                                 }
  156.                             });
  157.                         }
  158.                     });
  159.                 };
  160.                 func();
  161.             }
  162.             else {
  163.                 statusFunc(TITLE + "skipped");
  164.                 finishFunc();
  165.             }
  166.         },
  167.  
  168.         // memleak tests
  169.         function(statusFunc, errorFunc, finishFunc){
  170.             var TITLE = "MemLeak: ";
  171.             var id = "memleak";
  172.  
  173.             statusFunc(TITLE + "Started");
  174.             var ct = Xmarks.gSettings.getUnitTestOption(id);
  175.             if(ct){
  176.                 statusFunc(TITLE + "start (" + ct + ")");
  177.  
  178.                 var list = gFoxmarksUT.createTextArray(ct, 255, "all");
  179.                 for(var x = 0; x < ct; x++){
  180.                     if(x % 100 == 0){
  181.                         statusFunc(TITLE + "test #" + x);
  182.                     }
  183.                     var t = list[x];
  184.                     var s = Base64.encode(t, false);
  185.  
  186.                     if(t != Base64.decode(s)){
  187.                         errorFunc("FAIL: base64 with password '" + t1 + 
  188.                             "' and string '" + t2 + "'");
  189.                     }
  190.                     Components.utils.forceGC();
  191.                 }
  192.                 statusFunc(TITLE + "end");
  193.             }
  194.             else
  195.                 statusFunc(TITLE + "skipped");
  196.             statusFunc(TITLE + "Ended");
  197.             finishFunc();
  198.         },
  199.  
  200.         // Password tests
  201.         function(statusFunc, errorFunc, finishFunc){
  202.             var TITLE = "Password: ";
  203.             var id = "password";
  204.  
  205.             statusFunc(TITLE + "Started");
  206.             var ct = Xmarks.gSettings.getUnitTestOption(id);
  207.             if(ct){
  208.                 statusFunc(TITLE + "start (" + ct + ")");
  209.  
  210.                 // create some random strings
  211.                 var list = gFoxmarksUT.createTextArray(ct, 32, "alpha");
  212.                 var len;
  213.  
  214.                 //Remove all the old passwords
  215.                 var username = Xmarks.gSettings.username;
  216.                 var password = Xmarks.gSettings.passwordNoPrompt;
  217.                 var pin = Xmarks.gSettings.pinNoPrompt;
  218.                 var mgr = Components.classes["@mozilla.org/login-manager;1"]
  219.                     .getService(Components.interfaces.nsILoginManager);
  220.                 
  221.                 mgr.removeAllLogins();
  222.                 Xmarks.gSettings.username = username;
  223.                 Xmarks.gSettings.password = password;
  224.                 Xmarks.gSettings.pin = pin;
  225.  
  226.                 var addLogins = function(thelist){
  227.                     // Add logins with random values
  228.                     for(var x = 0; x < thelist.length; x++){
  229.                         var txt = thelist[x];
  230.                         var loginInfo = Components.classes[
  231.                             "@mozilla.org/login-manager/loginInfo;1"].
  232.                             createInstance(Components.interfaces.nsILoginInfo);
  233.                         loginInfo.init(
  234.                             txt + ".xmarks.com", null, 
  235.                             "Xmarks Unit Test", 
  236.                             txt, 
  237.                             txt,
  238.                             "", 
  239.                             "");
  240.                         try {
  241.                             mgr.addLogin(loginInfo);
  242.                         }
  243.                         catch(e) {}
  244.                     }
  245.                 };
  246.  
  247.                 addLogins(list);
  248.                 var server = new SyncServer();
  249.                 server.manual = true;
  250.                 server.upload(function(status){
  251.                     if(status){
  252.                         errorFunc(TITLE + "upload returned" + status);
  253.                     }
  254.                     else {
  255.                         server.download(function(status){
  256.                             if(status){
  257.                                 errorFunc(TITLE + "upload returned" + status);
  258.                             }
  259.                             else {
  260.                                 // todo, compare results
  261.  
  262.                                 var append = gFoxmarksUT.
  263.                                     createTextArray(25, 32, "alpha");
  264.                                 addLogins(append);
  265.                                 server.merge(true, function(status){
  266.                                     if(status){
  267.                                         errorFunc(TITLE + "merge returned " +
  268.                                             status);
  269.                                     }
  270.                                     else {
  271.                                         statusFunc(TITLE + "finished");
  272.                                         finishFunc();
  273.                                     }
  274.                                 });
  275.                             }
  276.                         });
  277.                     }
  278.                 });
  279.             }
  280.             else
  281.                 statusFunc(TITLE + "skipped");
  282.             statusFunc(TITLE + "Ended");
  283.         },
  284.     ],
  285.     runtest: function(id){
  286.         var self = this;
  287.         var statusFunc = function(s){
  288.             gFoxmarksUT.status(s);
  289.         };
  290.         var errorCallback = function(s){
  291.             gFoxmarksUT.numErrors++;
  292.             statusFunc(s);
  293.         };
  294.         var finishCallback = function(){
  295.             gFoxmarksUT.runNext();
  296.         };
  297.         this.testList[id](statusFunc, errorCallback, finishCallback);
  298.                    
  299.  
  300.     },
  301.     runNext: function(){
  302.         this.currTest++;
  303.         if(this.currTest >= this.testList.length){
  304.             this.status("End Unit Test. Num Errors = " + this.numErrors);
  305.         }
  306.         else {
  307.             var self = this;
  308.             var callbackSingle = {
  309.                 notify: function(){
  310.                     self.runtest(self.currTest);
  311.                 }
  312.             };
  313.             var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  314.             timer.initWithCallback(callbackSingle, 1000,
  315.                 Ci.nsITimer.TYPE_ONE_SHOT);
  316.         }
  317.     },
  318.     run: function(){
  319.         this.currTest = -1;
  320.         this.numErrors = 0;
  321.         this.status("Begin Unit Tests");
  322.         this.runNext();
  323.     }
  324. };
  325.